package com.jiyinit.web.core.business.service;

import com.jiyinit.core.framework.base.paging.PagingResult;
import com.jiyinit.web.core.business.dao.TabCoreTaskDao;
import com.jiyinit.web.core.business.dto.DailyLogRequest;
import com.jiyinit.web.core.business.dto.TaskRequest;
import com.jiyinit.web.core.business.entity.TabCoreTask;
import com.jiyinit.web.core.business.enums.CommonEnum;
import com.jiyinit.web.core.business.enums.OperationTypeConstant;
import com.jiyinit.web.core.business.enums.SystemConstant;
import com.jiyinit.web.core.business.vo.TaskInfoVo;
import com.jiyinit.web.core.common.util.MessagePush;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

/**
 * Created by lr on 2017/12/27.
 */
@Service("core.dailyLogService")
public class DailyLogServiceImpl {

    @Resource(name = "core.tabCoreTaskDao")
    private TabCoreTaskDao  tabCoreTaskDao;

    @Resource(name="send.message")
    private ReadMessageServiceImpl readMessageService;

    @Resource(name = "core.userTaskServiceImpl")
    private UserTaskServiceImpl userTaskService;

    @Resource(name ="core.jpushClientServiceImpl")
    private JpushClientServiceImpl jpushClientService;

    public PagingResult<TaskInfoVo> receiveDailyLogList(DailyLogRequest dailyLogRequest){
        return tabCoreTaskDao.receiveDailyLogList(dailyLogRequest);
    }


    public TabCoreTask getDailyLogById(Integer id){
        return tabCoreTaskDao.findTaskById(id);
    }

    public int add(DailyLogRequest request){
        //添加任务
        TabCoreTask dailyLog = new TabCoreTask();
        dailyLog.setTitle(request.getTitle());
        dailyLog.setDescription(request.getContent());
        dailyLog.setTaskCla(SystemConstant.DAILY_LOG);
        dailyLog.setCreateTime(new Date());
        dailyLog.setCreateUid(request.getUserId());
        dailyLog.setIsDel(CommonEnum.IsDelete.COMMON.getCode());
        int num = tabCoreTaskDao.addTask(dailyLog);
        //添加人员
        userTaskService.addUsers(request.getUserId(),request.getAlloterIds(),dailyLog.getTaskId());
        //未读表
        readMessageService.add(request.getUserId(),request.getAlloterIds(),SystemConstant.DAILY_LOG,dailyLog.getTaskId());
        //极光推送
        /*HashMap<String,String> sendMessages = new HashMap<>();
        sendMessages.put("content","新建工作日志");
        sendMessages.put("id",dailyLog.getTaskId().toString());
        sendMessages.put("taskCla",String.valueOf(SystemConstant.DAILY_LOG));

        String[] users = new String[request.getAlloterIds().length];
        for (int i = 0; i <request.getAlloterIds().length ; i++) {
        users[i] = request.getAlloterIds().toString();
    }

        jpushClientService.send(request.getChannel(),sendMessages,users);*/

        //极光推送消息
        Map<String,String> returnStr = new HashMap<>();
        returnStr.put("createUid", request.getUserId().toString());
        returnStr.put("taskCla", String.valueOf(SystemConstant.DAILY_LOG));
        returnStr.put("taskStatus","0");
        returnStr.put("content", "新增日志："+request.getTitle());
        returnStr.put("strikeType", "2");
        returnStr.put("taskId", String.valueOf(dailyLog.getTaskId()));
        returnStr.put("taskTitle", request.getTitle());
        returnStr.put("type", OperationTypeConstant.DAILYLOG_ADD);
        returnStr.put("dsc", request.getContent());

        Integer[] userIds=request.getAlloterIds();
        if(userIds!=null&&userIds.length>0)
        {
            StringBuffer sendUserIds = new StringBuffer();
            List<Integer> userIList=new ArrayList<Integer>();
            for(Integer userId: userIds){
                if(userIList.contains(userId))
                {
                    continue;
                }
                userIList.add(userId);
                sendUserIds.append(userId).append(",");
            }
            String sendUserStr =sendUserIds.substring(0,sendUserIds.toString().length()-1);
            String[] sendUserstemp = sendUserStr.split(",");
            String[] sendUsers = new String[sendUserstemp.length];
            System.arraycopy(sendUserstemp, 0, sendUsers, 0, sendUserstemp.length);

            if(request.getChannel()!=null&&request.getChannel().equals("01")){
                MessagePush messagePush = new MessagePush(returnStr,sendUsers);
            }else{
                MessagePush messagePush = new MessagePush(returnStr,sendUsers,"andriod");
            }
        }

        return num;
    }

    public TabCoreTask getDailyLogByUserId(Integer userId){
        return tabCoreTaskDao.getDailyLogByUserId(userId);
    }

//    public PagingResult<TabCoreTask> findByPage(DailyLogRequest dailyLogRequest){
//        return tabCoreTaskDao.findByPage(dailyLogRequest);
//    }

}
